package cn.wildfire.chat.app.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;

import butterknife.BindView;
import butterknife.OnClick;
import cn.wildfire.chat.kit.AppService;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfire.chat.kit.net.base.StatusResult;

import com.blankj.utilcode.util.GsonUtils;
import com.wildfire.chat.R;


public class RegisterActivity extends WfcBaseActivity {

    @BindView(R.id.etUserName) EditText etUserName;
    @BindView(R.id.etPassword) EditText etPassword;
    @BindView(R.id.etPasswordAgain) EditText etPasswordAgain;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        if (!isDarkTheme()) {
            setTitleBackgroundResource(cn.wildfire.chat.kit.R.color.white, false);
        }
    }

    private void register() {
        String username = etUserName.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        String passwordAgain = etPasswordAgain.getText().toString().trim();
        if (TextUtils.isEmpty(username)){
            return;
        }
        if (TextUtils.isEmpty(password)){
            return;
        }
        if (TextUtils.isEmpty(passwordAgain)){
            return;
        }
        if (password.length()<6){
            Toast.makeText(RegisterActivity.this, "请输入6位以上密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!password.equals(passwordAgain)){
            Toast.makeText(RegisterActivity.this, "两次输入密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }

        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .content("注册中...")
                .progress(true, 100)
                .cancelable(false)
                .build();
        dialog.show();

        AppService.Instance().accountRegister(username, password, passwordAgain,new AppService.CustomSimpleCallback() {
            @Override
            public void onUiSuccess(String result) {
                if (isFinishing()) {
                    return;
                }
//                {"code":17,"message":"用户名已经存在","result":null}
                StatusResult statusResult = GsonUtils.fromJson(result,StatusResult.class);
                if (statusResult.getCode()!=17) {
                    finish();
                }
                Toast.makeText(RegisterActivity.this, statusResult.getMessage(), Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }

            @Override
            public void onUiFailure(int code, String msg) {
                if (isFinishing()) {
                    return;
                }
                Toast.makeText(RegisterActivity.this, "注册失败：" + code + " " + msg, Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
    }
    @OnClick({R.id.registerButton})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.registerButton:
                register();
                break;
        }
    }
}